package cn.luckdou.system.controller.fast;

import cn.luckdou.PC.Hbean.ResultVo;
import cn.luckdou.WeChat.utils.AccessTokenUtils;
import cn.luckdou.commons.base.BaseController;
import cn.luckdou.commons.pojo.FilePojo;
import cn.luckdou.commons.utils.AliyunOSSClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/admin/fast")
public class FastController extends BaseController {

    /**
     * 文件上传至 阿里云OSS
     * @param request
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public ResultVo upload(HttpServletRequest request){
        ResultVo resultVo=new ResultVo();
        try {
            FilePojo uploads = AliyunOSSClientUtil.uploads(request);
            resultVo.setSuccessText();
            resultVo.setObj(uploads);
        } catch (IOException e) {
            e.printStackTrace();
            resultVo.setErrorText();
        }
        return resultVo;
    }

    /**
     * 获取微信AccsssToken
     * @return
     */
    @PostMapping("/getToken")
    @ResponseBody
    public ResultVo getToken(String type){
        ResultVo resultVo=new ResultVo();
        String token ="";
        if ("1".equals(type)){
            token= AccessTokenUtils.getToken();
        }
        if ("2".equals(type)){
            token=AccessTokenUtils.getNewToken();
        }
        if (token!=null && token.length()>0 ){
            resultVo.setSuccessText("ok");
            resultVo.setObj(token);
        }else{
            resultVo.setErrorText("error");
        }
        return resultVo;
    }

}
